package com.zyk.leetcode;

import java.util.Stack;

/**
 * @author zhangsan
 * @date 2021/4/9 11:06
 */
public class M0304 {

    static class MyQueue {

        Stack<Integer> elements;
        Stack<Integer> helpPop;

        /** Initialize your data structure here. */
        public MyQueue() {
            elements = new Stack<>();
            helpPop = new Stack<>();
        }

        /** Push element x to the back of queue. */
        public void push(int x) {
            elements.push(x);
        }

        // 每一次pop都会做一次颠倒, 和还原的过程. 其实不必还原也可以.因为这个辅助删除的都是接下来要出的
        /** Removes the element from in front of queue and returns that element. */
        public int pop() {
            while (!elements.isEmpty()) {
                helpPop.push(elements.pop());
            }
            int v = helpPop.pop();
            while (!helpPop.isEmpty()) {
                elements.push(helpPop.pop());
            }
            return v;
        }

        /** Get the front element. */
        public int peek() {
            return elements.firstElement();
        }

        /** Returns whether the queue is empty. */
        public boolean empty() {
            return elements.isEmpty();
        }
    }

    static class MyQueue2 {

        Stack<Integer> elements, helpPop;
        /** Initialize your data structure here. */
        public MyQueue2() {
            elements = new Stack<>();
            helpPop = new Stack<>();
        }

        /** Push element x to the back of queue. */
        public void push(int x) {
            elements.push(x);
        }

        /** Removes the element from in front of queue and returns that element. */
        public int pop() {
            if(helpPop.isEmpty()) {
                while (!elements.isEmpty()) {
                    helpPop.push(elements.pop());
                }
            }
            return helpPop.pop();
        }

        /** Get the front element. */
        public int peek() {
            if(helpPop.isEmpty()) {
                while (!elements.isEmpty()) {
                    helpPop.push(elements.pop());
                }
            }
            return helpPop.peek();
        }

        /** Returns whether the queue is empty. */
        public boolean empty() {
            return elements.isEmpty() && helpPop.isEmpty();
        }
    }

}
